import React from 'react';

// loading组件
const Loading = () => <div>Loading</div>;

export function dynamic(loadComponent: any) {
  const LazyComponent = React.lazy(loadComponent);
  // const LazyComponent = lazy(loadComponent);
  return () => (
    <React.Suspense fallback={<Loading />}>
      <LazyComponent />
    </React.Suspense>
  );
}

function lazy(load: any) {
  return class extends React.Component<any, any> {
    state: any = { Component: null };
    componentDidMount() {
      load().then((result: any) => {
        this.setState({ Component: result.default });
      });
    }
    render() {
      let { Component } = this.state;
      return Component && <Component />;
    }
  };
}
